<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单词" prop="word">
        <el-input v-model="queryParams.word" placeholder="请输入单词" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="锁定" prop="onLineFlag">
        <el-select v-model="queryParams.onLineFlag" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['thesaurus:words:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['thesaurus:words:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['thesaurus:words:batchRemove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-lock" size="mini" :disabled="multiple" @click="online"
          v-hasPermi="['thesaurus:words:online']">锁定</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="单词" align="center" prop="word" />
      <el-table-column label="词性种类" align="center" prop="speechTag">
        <template slot-scope="scope">
          <el-tag v-for="tag in scope.row.speechTag" :key="scope.row.id+'_tag_'+tag.id" closable size="mini"
            class="custom-tag" @close="deleTag(tag.id)">
            {{tag.text}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="锁定" align="center" prop="onLineFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_state" :value="scope.row.onLineFlag" />
        </template>
      </el-table-column>
      <el-table-column label="图片数量" align="center" prop="imageCount" />
      <el-table-column label="音频数量" align="center" prop="audioCount" />
      <el-table-column label="添加日期" align="center" prop="gmtCreate" />
      <el-table-column label="操1作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" v-if="showBtnFun(scope.row)" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['thesaurus:words:edit']">修改</el-button>
          <el-button size="mini" type="text" v-if="showBtnFun(scope.row) && scope.row.onLineFlag==1"
            icon="el-icon-unlock" @click="online(scope.row)" v-hasPermi="['thesaurus:words:online']">锁定</el-button>
          <el-button size="mini" type="text" v-if="showBtnFun(scope.row)" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['thesaurus:words:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></add-or-update>
  </div>
</template>

<script>
  import {
    listWords,
    delWords,
    online,
    offline,
    deleWordSpeechTag
  } from "@/api/bus_web/thesaurus/words"
  import addOrUpdate from './all_input_word.vue'

  export default {
    name: "Words",
    dicts: ['common_state'],
    components: {
      addOrUpdate
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 单词管理表格数据
        wordsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          word: null,
          wordType: 1,
          onLineFlag: null,
        },
        addOrUpdateVisible: false // 新增／更新，弹窗visible状态
      };
    },
    created() {
      this.getList();
    },
    methods: {
      //控制按钮
      showBtnFun(row) {
        if (row.onLineFlag === 0) {
          if (this.$auth.hasPermi('thesaurus:words:online')) {
            return true
          }
        } else {
          return true
        }
        return false
      },
      /** 查询单词管理列表 */
      getList() {
        this.loading = true;
        listWords(this.queryParams).then(response => {
          this.wordsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(0)
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.id)
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除单词管理编号为"' + ids + '"的数据项？').then(function() {
          return delWords(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /**上线单词*/
      online(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认锁定单词管理编号为"' + ids + '"的数据项？').then(function() {
          return online(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("上线成功");
        }).catch(() => {});
      },
      /**下线单词*/
      offline(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认下线单词管理编号为"' + ids + '"的数据项？').then(function() {
          return offline(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("下线成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('thesaurus/words/export', {
          ...this.queryParams
        }, `words_${new Date().getTime()}.xlsx`)
      },

      deleTag(id) {
        this.$modal.confirm('是否确认删除该词性？').then(function() {
          return deleWordSpeechTag(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});

      }
    }
  };
</script>
<style scoped>
  .custom-tag {
    margin-right: 8px;
    /* 设置标签之间的间距 */
  }

  .custom-tag:last-child {
    margin-right: 0;
    /* 最后一个标签不需要右边距 */
  }
</style>
